{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt \n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个列表： 127.059,130.208,132.743,133.019,136.471,139.583,140.708,143.396,144.706,147.917,148.673,155.660 \n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([ 127.059,  130.208,  132.743,  133.019,  136.471,  139.583,\n",
       "        140.708,  143.396,  144.706,  147.917,  148.673,  155.66 ])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str_num = input('请输入一个列表： ')\n",
    "lst_1 = str_num.split(',')\n",
    "lst = [float(x) for x in lst_1]\n",
    "x0 = np.array(lst)\n",
    "x0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  127.059,   257.267,   390.01 ,   523.029,   659.5  ,   799.083,\n",
       "         939.791,  1083.187,  1227.893,  1375.81 ,  1524.483,  1680.143])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#2.874,3.278,3.337,3.39,3.679\n",
    "x1 = x0.cumsum()\n",
    "x1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[-192.16300000000001,\n",
       " -323.63850000000002,\n",
       " -456.51949999999999,\n",
       " -591.2645,\n",
       " -729.29150000000004,\n",
       " -869.4369999999999,\n",
       " -1011.4889999999999,\n",
       " -1155.54,\n",
       " -1301.8514999999998,\n",
       " -1450.1464999999998,\n",
       " -1602.3129999999996]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cot =  0\n",
    "B = []\n",
    "while  cot <= len(x1)-2:\n",
    "    s = (-1/2)*(x1[cot]) + (-1/2)*(x1[cot+1])\n",
    "    B.append(s)\n",
    "    cot = cot + 1\n",
    "B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "C = np.array([1]*(len(x1)-1))\n",
    "C"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "D = np.vstack((B,C))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[ -1.92163000e+02,   1.00000000e+00],\n",
       "        [ -3.23638500e+02,   1.00000000e+00],\n",
       "        [ -4.56519500e+02,   1.00000000e+00],\n",
       "        [ -5.91264500e+02,   1.00000000e+00],\n",
       "        [ -7.29291500e+02,   1.00000000e+00],\n",
       "        [ -8.69437000e+02,   1.00000000e+00],\n",
       "        [ -1.01148900e+03,   1.00000000e+00],\n",
       "        [ -1.15554000e+03,   1.00000000e+00],\n",
       "        [ -1.30185150e+03,   1.00000000e+00],\n",
       "        [ -1.45014650e+03,   1.00000000e+00],\n",
       "        [ -1.60231300e+03,   1.00000000e+00]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Bt = np.mat(D)\n",
    "BB = Bt.T\n",
    "BB"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[ 130.208],\n",
       "        [ 132.743],\n",
       "        [ 133.019],\n",
       "        [ 136.471],\n",
       "        [ 139.583],\n",
       "        [ 140.708],\n",
       "        [ 143.396],\n",
       "        [ 144.706],\n",
       "        [ 147.917],\n",
       "        [ 148.673],\n",
       "        [ 155.66 ]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Yy = np.mat(x0[1:])\n",
    "Y = Yy.T\n",
    "Y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "a_j = (BB.T*BB)**(-1)*BB.T*Y\n",
    "a = a_j.getA()[0][0]\n",
    "u = a_j.getA()[1][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-0.0165305612613 126.637069484\n"
     ]
    }
   ],
   "source": [
    "print(a,u)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入预测时期K13\n",
      "1835.786\n"
     ]
    }
   ],
   "source": [
    "k = int(input('请输入预测时期K'))\n",
    "x0_1 = x0[0]\n",
    "x1_k = (x0_1 - u/a)*np.exp(-a*(k-1))+u/a\n",
    "x1_k\n",
    "print('%3.3f' %x1_k) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-0.62, 127.059, 256.866, 388.837, 523.008, 659.415, 798.096, 939.088, 1082.43, 1228.162, 1376.322, 1526.952] [ 127.679  129.807  131.971  134.171  136.407  138.681  140.992  143.342\n",
      "  145.732  148.16   150.63 ] [ 127.059  130.208  132.743  133.019  136.471  139.583  140.708  143.396\n",
      "  144.706  147.917  148.673  155.66 ]\n"
     ]
    }
   ],
   "source": [
    "x1_j = []\n",
    "for x in range(len(x0)):\n",
    "    x0_1 = x0[0]\n",
    "    x1_k = (x0_1 - u/a)*np.exp(-a*(x-1))+u/a\n",
    "    x1_j.append('%3.3f' %x1_k)\n",
    "x1_j = [float(i) for i in x1_j]\n",
    "x1_j1 = np.array(x1_j[:-1])\n",
    "x1_j2 =  np.array(x1_j[1:])\n",
    "x0_j = x1_j2 - x1_j1\n",
    "print(x1_j,x0_j,x0)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.981%\n",
      "2.262%\n",
      "0.794%\n",
      "1.714%\n",
      "2.328%\n",
      "1.462%\n",
      "1.705%\n",
      "0.952%\n",
      "1.499%\n",
      "0.346%\n",
      "3.339%\n"
     ]
    }
   ],
   "source": [
    "q_k = x0[1:] - x0_j\n",
    "e_k = q_k/x0_j\n",
    "for x in e_k:\n",
    "    print(\"%3.3f%%\" %(100*x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "for x in e_k:\n",
    "    print(\"%3.3f%%\" %(100*x))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
